package com.ssd4.uidesign.util;
import java.io.*;
import java.util.Enumeration;
import java.util.zip.*;

public class UnZipFile{

	/**
	* 进行压缩包的解压
	* @param zipFileName
	*     需要解压的文件地址(如：E:/a.zip)
	* @param extPlace
	*     解压后的文件地址(如:E:/)
	* @throws Exception
	*/
	public static void unzip(String zipFileName, String extPlace) throws Exception { 
		try {  
			System.out.println("mkdir "+extPlace); 
			(new File(extPlace)).mkdirs();   
			File f = new File(zipFileName);   
			ZipFile zipFile = new ZipFile(zipFileName);   
			if((!f.exists()) && (f.length() <= 0)) {throw new Exception("要解压的文件不存在!");}   
			String strPath, gbkPath, strtemp;   
			File tempFile = new File(extPlace);   
			strPath = tempFile.getAbsolutePath();   
			Enumeration e = zipFile.entries();          //.getEntries();   
			while(e.hasMoreElements()){   
			ZipEntry zipEnt = (ZipEntry) e.nextElement();   
			gbkPath=zipEnt.getName();
			System.out.println(gbkPath);
			if(zipEnt.isDirectory()){   
				strtemp = strPath + File.separator + gbkPath;   
				File dir = new File(strtemp);   
				dir.mkdirs();   
				continue;   
			} else {   
				//读写文件   
				InputStream is = zipFile.getInputStream(zipEnt);   
				BufferedInputStream bis = new BufferedInputStream(is);   
				gbkPath=zipEnt.getName();   
				strtemp = strPath + File.separator + gbkPath;   
				//建目录   
				String strsubdir = gbkPath; 
				//System.out.println(gbkPath);
				//System.out.println(strtemp);
				for(int i = 0; i < strsubdir.length(); i++) {   
					if(strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {   
						String temp = strPath + File.separator + strsubdir.substring(0, i);
						System.out.println(temp);
						File subdir = new File(temp);
						System.out.println(subdir.getParent()+"......"+subdir.getName());
						if(!subdir.exists())   
						subdir.mkdir();   
					}   
				}   
				FileOutputStream fos = new FileOutputStream(strtemp);   
				BufferedOutputStream bos = new BufferedOutputStream(fos);   
				int c;   
				while((c = bis.read()) != -1) {   
					bos.write((byte) c);   
				}   
				bis.close();
				is.close();
				bos.flush();
				fos.flush();
				bos.close();  
				fos.close();   
			}   
		}   
	} catch(Exception e) {   
		e.printStackTrace();   
		throw e;   
		}   
		
	}

	public  static void unZip(String zipFileName,String extPlace)throws Exception{
    	unzip(zipFileName, extPlace);
    }
}